package com.itheima.store.service.impl;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.itheima.store.dao.BookBorrowDao;
import com.itheima.store.dao.BookBorrowItemsDao;
import com.itheima.store.dao.BookCategoryDao;
import com.itheima.store.dao.BookDao;
import com.itheima.store.domain.Book;
import com.itheima.store.domain.BookBorrow;
import com.itheima.store.domain.PageBean;
import com.itheima.store.service.BookBorrowService;
import com.itheima.store.utils.BeanFactory;

public class BookBorrowServiceImpl implements BookBorrowService {

	@Override
	public String addBookBorrow(BookBorrow bb) throws SQLException {
		
		BookBorrowDao bbd =(BookBorrowDao)BeanFactory.getBean("bookBorrowDao");
		return bbd.addBookBorrow(bb);
	}

	@Override
	public PageBean<BookBorrow> findAppByPageuserId(String user_id, Integer currPage) throws SQLException{
		PageBean<BookBorrow> pageBean = new PageBean<BookBorrow>();
		// 设置参数：
		// 设置当前页数：
		pageBean.setCurrPage(currPage);
		// 设置每页显示的记录数:
		Integer pageSize = 10;
		pageBean.setPageSize(pageSize);
		// 设置总记录数:
		BookBorrowDao bbd = (BookBorrowDao) BeanFactory.getBean("bookBorrowDao");
		Integer totalCount = bbd.findCountByUserId(user_id);
		pageBean.setTotalCount(totalCount);
		// 设置总页数:
		double tc = totalCount;
		Double num = Math.ceil(tc / pageSize);
		pageBean.setTotalPage(num.intValue());
		// 设置每页显示的数据的集合:
		int begin = (currPage - 1) * pageSize;
		List<BookBorrow> list = bbd.findPageByUserId(user_id,begin,pageSize);
		pageBean.setList(list);
		return pageBean;
	}

	@Override
	public void removeCart(String book_id) throws SQLException {
		BookBorrowDao bbd = (BookBorrowDao) BeanFactory.getBean("bookBorrowDao");
		bbd.removeCart(book_id);
		
	}

	@Override
	public BookBorrow findAppBybookBorrId(String bookBorrowing_id) throws SQLException {
		BookBorrowDao bbd = (BookBorrowDao) BeanFactory.getBean("bookBorrowDao");
		
		return bbd.findAppBybookBorrId(bookBorrowing_id);
	}

	@Override
	public void updateBookBorrow(BookBorrow bb) throws SQLException {
		BookBorrowDao bbd = (BookBorrowDao) BeanFactory.getBean("bookBorrowDao");
		bbd.updateBookBorrow(bb);
		
	}

	@Override
	public void updateBookBorrow(String bookBorrowing_id, String total) throws SQLException {
		BookBorrowDao bbd = (BookBorrowDao) BeanFactory.getBean("bookBorrowDao");
		bbd.updateBookBorrow(bookBorrowing_id,total);
		
	}

	@Override
	public void deleteBookBorrAllApp(String user_id) throws SQLException {
		BookBorrowDao bbd = (BookBorrowDao) BeanFactory.getBean("bookBorrowDao");
		bbd.deleteBookBorrAllApp(user_id);
	}

	@Override
	public List findAppByUserId(String user_id) throws SQLException {
		BookBorrowDao bbd = (BookBorrowDao) BeanFactory.getBean("bookBorrowDao");
		
		return bbd.findAppByUserId(user_id);
		
	}

	@Override
	public PageBean<BookBorrow> findAllApp(Integer bstatus,Integer currPage) throws SQLException {
		PageBean<BookBorrow> pageBean = new PageBean<BookBorrow>();
		// 设置参数：
		// 设置当前页数：
		pageBean.setCurrPage(currPage);
		// 设置每页显示的记录数:
		Integer pageSize = 12;
		pageBean.setPageSize(pageSize);
		// 设置总记录数:
		BookBorrowDao bbd = (BookBorrowDao) BeanFactory.getBean("bookBorrowDao");
		Integer totalCount = bbd.findCount(bstatus);
		pageBean.setTotalCount(totalCount);
		// 设置总页数:
		double tc = totalCount;
		Double num = Math.ceil(tc / pageSize);
		pageBean.setTotalPage(num.intValue());
		// 设置每页显示的数据的集合:
		int begin = (currPage - 1) * pageSize;
		List<BookBorrow> list = bbd.findPageByStatus(bstatus,begin,pageSize);
		for(int i=0;i<list.size();i++){
			BookBorrow obj = list.get(i);
			
			Date date =  obj.getExpeReturnTime();
			Long expeReturnTime = date.getTime();
			Long currTime = Calendar.getInstance().getTimeInMillis();
			int days = (int) ((currTime-expeReturnTime)/(1000 * 60 * 60 * 24)); 
			int renewDays = obj.getRenewDays();
			int day = days-renewDays;
			obj.setExceTime(day);
			
		}               
		pageBean.setList(list);
		return pageBean;
	}

	@Override
	public void updateBookBorrowStatus(String bookBorrowing_id,String flag) throws SQLException {
		BookBorrowDao bbd = (BookBorrowDao) BeanFactory.getBean("bookBorrowDao");
		bbd.updateBookBorrowStatus(bookBorrowing_id,flag);
		
	}

	
	@Override
	public List<BookBorrow> findByOid(String oids) throws SQLException {
		BookBorrowDao bbd = (BookBorrowDao) BeanFactory.getBean("bookBorrowDao");
 		return bbd.findByOid(oids);
	}

	@Override
	public void updateRenewDays(String bookBorrowing_id, String renewDays) throws SQLException {
		// TODO Auto-generated method stub
		BookBorrowDao bbd = (BookBorrowDao) BeanFactory.getBean("bookBorrowDao");
		bbd.updateRenewDays(bookBorrowing_id,renewDays);
	}

	@Override
	public BookBorrow findByBookId(String books_id, String user_id) throws SQLException {
		BookBorrowDao bbd = (BookBorrowDao) BeanFactory.getBean("bookBorrowDao");
		
		return bbd.findByBookId(books_id,user_id);
	}

	@Override
	public void updateTotal(String bookBorrowing_id, Integer total, Date expeReturnTime) throws SQLException {
		BookBorrowDao bbd = (BookBorrowDao) BeanFactory.getBean("bookBorrowDao");
		bbd.updateTotal(bookBorrowing_id,total,expeReturnTime);
		
	}

	@Override
	public void updateBookTotal(String book_id, Integer tots,Integer flag) throws SQLException {
		BookBorrowDao bbd = (BookBorrowDao) BeanFactory.getBean("bookBorrowDao");
		bbd.updateBookTotal(book_id,tots,flag);
		
	}
}
